package com.y3k.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.y3k.utils.ConnectionHelper;

public class IpDao {

	public static final Logger log = Logger.getLogger(IpDao.class);

	public boolean check(String ip) throws Exception {
		boolean exist = false;
		Connection conn = ConnectionHelper.getConnection();
		Statement stmt = conn.createStatement();

		String allowSQL = "select ip from ipallow where ip='" + ip + "'";
		log.info(allowSQL);
		String denySQL = "insert into ipdeny (ip) values ('" + ip + "') ";
		log.info(denySQL);

		ResultSet rs = stmt.executeQuery(allowSQL);
		if (rs.next()) {
			log.info("Allow [" + ip + "]");
			exist = true;
		} else {
			log.info("Deny [" + ip + "]");
			stmt.executeUpdate(denySQL);
			conn.commit();
		}
		stmt.close();

		return exist;
	}

}